2048 遊戲每次玩家滑動後,會在盤面隨機空格生成一個新的數字(通常是 2 或 4)。
今天我們要實作這個「隨機新增數字」的邏輯,讓遊戲盤面逐步變化,帶來挑戰性。
package internal
// sideSize - 預設 sideSize
const sideSize = 4
// randomPositioner - 根據給訂的 TotalSize 隨機產生一個位置
type randomPositoner func(TotalSize int) int
// randomGenerator - 隨機給個 0 - 1 之間的機率數
type randomGenerator func() float64
// Game - 紀錄當下遊戲處理狀態
//
// board [][]int - 紀錄盤面狀態
type Game struct {
board [][]int
randomPositonerFunc randomPositoner
randomFunc randomGenerator
}
// Init - 初始化
func (g *Game) Init(data [][]int, randomPosFunc randomPositoner, randomFunc randomGenerator) {
// setup random functions
g.randomPositonerFunc = randomPosFunc
g.randomFunc = randomFunc
// 建立棋盤
g.board = make([][]int, sideSize)
for index := range g.board {
g.board[index] = make([]int, sideSize)
}
// checkout input value
if len(data) != sideSize || len(data[0]) != sideSize {
return
}
// setup data
for r := range sideSize {
for c := range sideSize {
if data[r][c] != 0 {
g.board[r][c] = data[r][c]
}
}
}
}
// addRandomTile - 新增隨機的 2 或是 4
func (g *Game) addRandomTile() {
// 蒐集所有空的 tile
emptyTiles := make([][2]int, 0, sideSize*sideSize)
for r := 0; r < sideSize; r++ {
for c := 0; c < sideSize; c++ {
if g.board[r][c] == 0 {
emptyTiles = append(emptyTiles, [2]int{r, c})
}
}
}
// 如果所有格子都滿了
if len(emptyTiles) == 0 {
return
}
// 選出要填入的位置
position := emptyTiles[g.randomPositonerFunc(len(emptyTiles))]
// 90% 機率是 2 , 10% 機率則為 4
value := 2
if g.randomFunc() < 0.1 {
value = 4
}
g.board[position[0]][position[1]] = value
}
func NewGame() *Game {
return &Game{
nil,
defaultRandomPositioner,
defaultRandomFunc,
}
}
package internal
import (
"math/rand"
"time"
)
var defaultRandomPositioner randomPositoner = func(totalSize int) int {
if totalSize == 1 {
return 0
}
rand.New(rand.NewSource(time.Now().UnixNano()))
return rand.Intn(totalSize)
}
var defaultRandomFunc randomGenerator = func() float64 {
rand.New(rand.NewSource(time.Now().UnixNano()))
return rand.Float64()
}
gemini -p "請幫我產生 2048 遊戲中「隨機新增數字」功能的測試案例。
條件如下:
1. 盤面為 4x4 二維整數陣列,0 代表空格。
2. 每次新增數字會在隨機空格新增 2 或 4,機率約 90% 產生 2,10% 產生 4。
3. 若盤面無空格則不新增數字。
請產生至少 5 組盤面初始狀態與新增數字後的狀態,
並標註新增的數字位置與數值。
範例格式:
初始盤面:
0 0 0 0
0 2 0 0
0 0 0 0
0 0 0 4
新增結果(新增數字 2 在位置 [0,2]):
0 0 2 0
0 2 0 0
0 0 0 0
0 0 0 4
請依此格式列出 5 組測試案例。"
這裏是 5 組 2048 遊戲「隨機新增數字」功能的測試案例:
初始盤面:
0 0 0 0
0 2 0 0
0 0 0 0
0 0 0 4
新增結果(新增數字 2 在位置 [0,1]):
0 2 0 0
0 2 0 0
0 0 0 0
0 0 0 4
初始盤面:
2 4 8 16
32 64 128 256
512 1024 2048 0
2 4 8 16
新增結果(新增數字 4 在位置 [2,3]):
2 4 8 16
32 64 128 256
512 1024 2048 4
2 4 8 16
初始盤面:
2 4 2 4
4 2 4 2
2 4 2 4
4 2 4 0
新增結果(新增數字 2 在位置 [3,3]):
2 4 2 4
4 2 4 2
2 4 2 4
4 2 4 2
初始盤面:
8 16 32 2
4 2 8 4
16 32 4 8
2 4 16 0
新增結果(新增數字 2 在位置 [3,3]):
8 16 32 2
4 2 8 4
16 32 4 8
2 4 16 2
初始盤面:
2 4 8 16
32 64 128 256
512 1024 2048 2
4 8 16 32
新增結果(盤面無空格,不新增數字):
2 4 8 16
32 64 128 256
512 1024 2048 2
4 8 16 32
https://github.com/leetcode-golang-classroom/2048-game/actions/runs/17023951933/job/48256828565
明天我們將開始挑戰 滑動與合併邏輯的實作,
先從「向左滑動」開始,實現 2048 最關鍵的遊戲玩法